/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.openbravo.data.loader;

import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataWrite;
import com.openbravo.data.loader.Datas;
import com.openbravo.format.Formats;
/**
 *
 * @author Administrator
 */
public class BatchPropertyData {
    private static String[] DATATYPE={"INT","STRING","DOUBLE","BOOLEAN","TIMESTAMP","BYTES","IMAGE","SERIALIZABLE"};
    private Datas datatype;
    private Object value;

    public BatchPropertyData( Object value,Datas datatype) {
        this.datatype = datatype;
        this.value = value;
    }

    public Datas getDatatype() {
        return datatype;
    }

    public void setDatatype(Datas datatype) {
        this.datatype = datatype;
    }

    public Object getValue() {
        return value;
    }

    public void setValue(Object value) {
        this.value = value;
    }

    void setValue(DataWrite dp, int i) throws BasicException {
        datatype.setValue(dp, i, value);
    }

    static BatchPropertyData getPropertyData(String dataType,String value1){
        int index=99;
        for(int i=0;i<DATATYPE.length;i++){
            if(DATATYPE[i].equalsIgnoreCase(dataType)){
                index=i;
                break;
            }
        }
        //System.out.println("index:"+index);
        switch (index) {
            case 0:
                return new BatchPropertyData(Integer.valueOf(value1), Datas.INT);
            case 1:
                return new BatchPropertyData(value1, Datas.STRING);
            case 2:
                return new BatchPropertyData(Double.valueOf(value1), Datas.DOUBLE);
            case 3:
                return new BatchPropertyData(Boolean.valueOf(value1), Datas.BOOLEAN);
            case 4:
                try {
                    return new BatchPropertyData(Formats.TIMESTAMP.parseValue(value1), Datas.TIMESTAMP);
                } catch (BasicException ex) {
                    return new BatchPropertyData(null, Datas.TIMESTAMP);
                }
            case 5:
                return new BatchPropertyData(value1.getBytes(), Datas.BYTES);
        }
        return null;
    }

    @Override
    public String toString() {
        return String.valueOf(value)+"type:"+datatype.toString();
    }


}
